package com.evon.yardmanagement.transformer;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;

import com.evon.yardmanagement.model.BaseModel;

public class BaseTransformer<T extends BaseModel, E> {

	static DozerBeanMapper mapper = new DozerBeanMapper();

	Class<T> classModelType;
	Class<E> classDTOType;

	public BaseTransformer(Class<T> classTypeModel, Class<E> classTypeDTO) {
		this.classModelType = classTypeModel;
		this.classDTOType = classTypeDTO;
	}

	public T transformToModel(E src) {
		T newInstance;
		try {
			newInstance = classModelType.newInstance();

			mapper.map(src, newInstance);
			return newInstance;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public T transformToModel(E src, T destination) {
		mapper.map(src, destination);
		return destination;

	}

	public E transformToDTO(T src) {
		E newInstance;
		try {
			newInstance = classDTOType.newInstance();

			mapper.map(src, newInstance);
			return newInstance;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public List<T> transformToModel(List<E> src) {

		List<T> destinationList = new ArrayList<T>();
		try {
			for (E e : src) {
				T newInstance = classModelType.newInstance();
				mapper.map(e, newInstance);
				destinationList.add(newInstance);
			}
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return destinationList;

	}

	public List<E> transformToDTO(List<T> src) {

		List<E> destinationList = new ArrayList<E>();
			for (T t : src) {
				//E newInstance = classDTOType.newInstance();
				E newInstance = mapper.map(t, classDTOType);
				destinationList.add(newInstance);
			}
		return destinationList;

	}

}
